package com.candy.candymember.controller;

import com.candy.candycommon.utils.ApplicationContentUtils;
import com.candy.candycommon.utils.ResultBean;
import com.candy.candymember.config.RedisUtils;
import com.candy.candymember.domain.Member;
import com.candy.candymember.event.ScoreEvent;
import com.candy.candyorderrpc.service.OrderService;
import com.candy.candyproductservice.service.QuestionService;
import com.gitee.candy.configer.customInterface.HandlingTime;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author suYanLong
 * @date 2022年09月29日 13:36
 */
@Slf4j
@RestController
@RequestMapping("member")
//@AllArgsConstructor
@HandlingTime
public class MemberController {

    @Resource
    OrderService orderService;

    @Lazy
    @Resource
    QuestionService questionService;

    @Autowired
    StringRedisTemplate redisTemplate;

    BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();

    @PostMapping("getMember")
    public ResultBean getMember(@RequestBody Member member) {
        return ResultBean.ok(member);
    }

    //    http://localhost:8086/candy-member/member/getOrder
    @GetMapping("getOrder")
    public ResultBean getOrder() {
        log.info("=============getOrder============");
        return orderService.getByOrderId(1l);
    }

    @PostConstruct
    public void test() {
        redisTemplate.opsForValue().set("suyl", "123");
        Thread thread = new Thread(() -> {
            for (; ; ) {
                try {
                    String take = blockingDeque.take();
                    log.info(" take = {}", take);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (false)
                    break;
            }

        });
        thread.start();
    }

    //    http://localhost:8086/candy-member/member/publishScoreEvent
    @GetMapping("publishScoreEvent")
    public ResultBean publishScoreEvent() {
        log.info("=============getOrder============");
        ApplicationContentUtils.getApplicationContext().publishEvent(new ScoreEvent("event", "candy", 100));
        blockingDeque.add("publishScoreEvent");
        return ResultBean.ok();
    }

    int total = 100;

    int totalNo = 100;

    @Resource
    private RedisUtils redisUtils;

    Random random = new Random();

    @GetMapping("distributedLocksNo")
    public ResultBean distributedLocksNo() throws InterruptedException {
//        Thread.sleep(random.nextInt(5));
        if (totalNo > 0) {
            totalNo = --totalNo;
//            totalNo = (totalNo - 1);
//            --totalNo;
        } else {
            log.error("totalNo已经调用完成");
        }
//        Thread.sleep(random.nextInt(5));
        return ResultBean.ok(totalNo);
    }

    @GetMapping("distributedLocks")
    public ResultBean distributedLocks() throws InterruptedException {
        // redisson分布式锁
        RLock lock = redisUtils.getLock("total");
        lock.lock();
        if (total > 0)
            --total;
        else
            log.error("total已经调用完成");
        lock.unlock();
        return ResultBean.ok(total);
    }

}
